/*
 * FXGL - JavaFX Game Library. The MIT License (MIT).
 * Copyright (c) AlmasB (almaslvl@gmail.com).
 * See LICENSE for details.
 */

package com.windea.study.kotlin.fxgl.sample.sandbox

import com.almasb.fxgl.app.*
import com.almasb.fxgl.dsl.*
import com.almasb.fxgl.input.*
import com.almasb.fxgl.input.view.*
import com.almasb.fxgl.input.virtual.*
import javafx.beans.binding.*
import javafx.scene.*
import javafx.scene.input.*
import javafx.scene.layout.*
import javafx.scene.paint.*
import javafx.scene.shape.*

/**
 * @author Almas Baimagambetov (almaslvl@gmail.com)
 */
class VirtualControllerSample : GameApplication() {
	override fun initSettings(settings: GameSettings) {
		settings.width = 850
		settings.height = 700
		settings.isMenuEnabled = false
	}
	
	override fun initInput() {
		getInput().addAction(object : UserAction("test") {
			override fun onActionBegin() {
				println("start f")
			}
			
			override fun onAction() {
				println("f")
			}
			
			override fun onActionEnd() {
				println("end f")
			}
		}, KeyCode.F, VirtualButton.Y)
	}
	
	override fun initGame() {
		getGameScene().setBackgroundColor(
			LinearGradient(0.5, 0.0, 0.5, 1.0, true, CycleMethod.NO_CYCLE,
				Stop(0.1, Color.WHITE),
				Stop(0.39, Color.GRAY),
				Stop(0.95, Color.BLACK)
			)
		)
	}
	
	override fun initUI() {
		val dpad = getInput().createVirtualDpadView(CustomDpad())
		val buttons = getInput().createPSVirtualControllerView()
		
		addUINode(dpad, 50.0, 20.0)
		addUINode(buttons, 200.0, 150.0)
		
		addUINode(getInput().createXboxVirtualControllerView(), 350.0, 20.0)
		
		addUINode(getInput().createVirtualDpadView(), 500.0, 150.0)
		
		addUINode(getInput().createVirtualMenuKeyView(getSettings().menuKey, getSettings().isMenuEnabled), 650.0, 50.0)
		
		val keyView = KeyView(KeyCode.E)
		keyView.keyColorProperty().bind(
			Bindings.`when`(keyView.hoverProperty()).then(Color.GREEN).otherwise(Color.GRAY)
		)
		keyView.backgroundColorProperty().bind(
			Bindings.`when`(keyView.hoverProperty()).then(Color.WHITE).otherwise(Color.BLACK)
		)
		
		addUINode(keyView, 50.0, 400.0)
		
		val mouseView = MouseButtonView(MouseButton.PRIMARY)
		mouseView.backgroundColor = Color.BLUEVIOLET
		mouseView.colorProperty().bind(
			Bindings.`when`(mouseView.hoverProperty()).then(Color.GREEN).otherwise(Color.GRAY)
		)
		//        mouseView.backgroundColorProperty().bind(
		//                Bindings.when(mouseView.hoverProperty()).then(Color.WHITE).otherwise(Color.BLACK)
		//        );
		
		addUINode(mouseView, 150.0, 400.0)
		
		val triggerView = TriggerView(KeyTrigger(KeyCode.K))
		triggerView.colorProperty().bind(
			Bindings.`when`(triggerView.hoverProperty()).then(Color.YELLOW).otherwise(Color.AZURE)
		)
		
		addUINode(triggerView, 350.0, 400.0)
		
		// special keys
		addUINode(HBox(10.0,
			KeyView(KeyCode.UP),
			KeyView(KeyCode.RIGHT),
			KeyView(KeyCode.DOWN),
			KeyView(KeyCode.LEFT),
			KeyView(KeyCode.SHIFT),
			KeyView(KeyCode.CONTROL),
			KeyView(KeyCode.ALT),
			KeyView(KeyCode.DELETE),
			KeyView(KeyCode.SPACE),
			KeyView(KeyCode.ENTER, Color.BLUE)
		), 50.0, 550.0)
	}
	
	class CustomDpad : VirtualDpad(getInput()) {
		
		override fun createView(btn: VirtualButton): Node {
			return Rectangle(40.0, 40.0, Color.RED)
		}
	}
}

fun main(args: Array<String>) {
	GameApplication.launch(VirtualControllerSample::class.java, args)
}
